package com.app.public_dependency.response;

import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangzy
 * @description 封装处理结果
 */
public class Result extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Result() {
        put("code", HttpStatus.OK.value());
        put("message", HttpStatus.OK.getReasonPhrase());
    }

    public static Result success() {
        return new Result();
    }

    public static Result success(int code) {
        Result result = new Result();
        result.put("code", code);
        return result;
    }

    public static Result success(int code, String message) {
        Result result = new Result();
        result.put("code", code);
        result.put("message", message);
        return result;
    }

    public static Result success(String message) {
        Result result = new Result();
        result.put("message", message);
        return result;
    }

    public static Result error() {
        return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    public static Result error(int code) {
        Result result = new Result();
        result.put("code", code);
        result.put("message", null);
        return result;
    }

    public static Result error(int code, String message) {
        Result result = new Result();
        result.put("code", code);
        result.put("message", message);
        return result;
    }

    public Result data(Map<String, Object> data) {
        this.putAll(data);
        return this;
    }

    public Result data(String key, Object data) {
        this.put(key, data);
        return this;
    }

    public int getCode() {
        return (int) this.get("code");
    }

}